// pages/user_info/userinfo.js
const app = getApp()
const util = require('../../utils/util');
const api = require('../../utils/api');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    userhead: '',
    username: '',
    usergender: '',
    good: '',
    order: '',
    addnum: 0,
    addprice: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let userInfo = wx.getStorageSync('userInfo');
    this.setData({
      userhead: userInfo.avatar,
      username: userInfo.nickname,
      usergender: userInfo.gender == 1 ? '男' : '女'
    })

    util.myRequest(api.index, {}, res => {
      this.setData({
        good: res.data.data.hotGoodsList
      })
    }, );

    //获取订单列表
    let that = this;
    let order = wx.getStorageSync('order');

    //计算商品总数，总额
    if (order) {
      let num = 0,
        price = 0;
      let that = this;
      order.forEach(function (item, index) {
        num += item.number
        price += item.retail_price * item.number
      })
      that.setData({
        addnum: num,
        addprice: price
      })
      wx.setStorageSync('addnum', that.data.addnum);
      wx.setStorageSync('addprice', that.data.addprice);
    }
  }
})